
/**		
 * 
 *	uk.co.lmfm.guppy.view.components.project.ProjectView
 *	
 *	@version 1.00 | Jan 14, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view.components.project 
{
	import uk.co.lmfm.guppy.view.components.events.ProjectViewEvent;
	import uk.co.lmfm.guppy.model.vo.ProjectVO;
	import uk.co.lmfm.guppy.view.components.core.AbstractView;
	import uk.co.lmfm.guppy.view.components.core.IViewComponent;

	import com.bit101.components.PushButton;

	import org.osflash.thunderbolt.Logger;

	import flash.events.MouseEvent;

	/**
	 * ProjectView
	 */
	public class ProjectView extends AbstractView implements IViewComponent
	{
		//	----------------------------------------------------------------
		//	PRIVATE MEMBERS
		//	----------------------------------------------------------------

		private var _settingsPanel : ProjectSettingsView = new ProjectSettingsView();		private var _configPanel : ProjectConfigView = new ProjectConfigView();
		private var _libraryPanel : ProjectLibraryView = new ProjectLibraryView();
		private var _createButton : PushButton = new PushButton();

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function ProjectView()
		{
			super();
			
			_createButton.label = "Create Project";
			
			addChild(_settingsPanel);			addChild(_configPanel);			addChild(_libraryPanel);			addChild(_createButton);
			
			configureListeners();
		}

		//	----------------------------------------------------------------
		//	PUBLIC METHODS
		//	----------------------------------------------------------------

		override public function show() : void
		{
			super.show();
			
			_settingsPanel.show();			_configPanel.show();
			_libraryPanel.show();
			
			resize();
		}

		//	----------------------------------------------------------------
		//	PROTECTED METHODS
		//	----------------------------------------------------------------

		override protected function resize() : void
		{
			super.resize();
			
			_settingsPanel.y = 0;
			_configPanel.y = _settingsPanel.y + _settingsPanel.height;
			_libraryPanel.y = _configPanel.y + _configPanel.height;

			_createButton.x = stage.stageWidth - _createButton.width - 10;
			_createButton.y = stage.stageHeight - _createButton.height - 50;
		}

		//	----------------------------------------------------------------
		//	PRIVATE METHODS
		//	----------------------------------------------------------------

		private function configureListeners() : void
		{
			_createButton.addEventListener(MouseEvent.CLICK, onButtonClicked);
		}

		private function createRequest() : void
		{
			var project : ProjectVO = new ProjectVO();
			
			project.name = _settingsPanel.projectName;
			project.mainClass = _settingsPanel.mainClass;
			project.rootPackage = _settingsPanel.rootPackage;
			project.playerVersion = _settingsPanel.playerVersion;
			project.workspace = _settingsPanel.workspace;
			project.folder = _settingsPanel.folder;
			
			project.isPureMVC = _configPanel.isPureMVC;
			
			project.libraries = _libraryPanel.selected;
			
			if(project.name.replace(/\s/g, '').length < 1)
			{
				Logger.error("You must enter a project name");
				return;
			}
			
			dispatchEvent(new ProjectViewEvent(ProjectViewEvent.CREATE_PROJECT, false, false, project));
		}

		//	----------------------------------------------------------------
		//	EVENT HANDLERS
		//	----------------------------------------------------------------

		private function onButtonClicked(event : MouseEvent) : void
		{
			switch(event.currentTarget)
			{
				case _createButton :
				
					createRequest();
				 
					break;
			}
		}

		//	----------------------------------------------------------------
		//	PUBLIC ACCESSORS
		//	----------------------------------------------------------------

		public function get settingsPanel() : ProjectSettingsView
		{
			return _settingsPanel;
		}

		public function get configPanel() : ProjectConfigView
		{
			return _configPanel;
		}

		public function get libraryPanel() : ProjectLibraryView
		{
			return _libraryPanel;
		}
	}
}
